<template>
  <div class="user-title-display">
    <div class="title-card">
      <div class="user-info">
        <div class="avatar">
          <span class="avatar-icon">👤</span>
        </div>
        <div class="user-details">
          <h3 class="username">{{ username || '未知用户' }}</h3>
          <div class="level-title" v-if="levelInfo">
            <span class="level-badge">LV. {{ levelInfo.currentLevel || 0 }}</span>
            <span v-if="levelInfo.currentTitle" class="title-separator">·</span>
            <TitleBadge 
              v-if="levelInfo.currentTitle"
              :title="levelInfo.currentTitle"
              :level="levelInfo.currentLevel || 1"
              size="sm"
              :active="true"
            />
          </div>
        </div>
      </div>
      
      <div class="titles-list" v-if="titles && titles.length > 0">
        <h4>可用称号</h4>
        <div class="titles-grid">
          <div 
            v-for="title in titles" 
            :key="title.id || title.title"
            class="title-item"
            :class="{ 'current': title.isCurrent }"
          >
            <TitleBadge :title="title.title" :level="title.requiredLevel || 1" size="sm" :active="title.isCurrent" />
            <span v-if="title.isCurrent" class="current-badge">当前</span>
          </div>
        </div>
      </div>
      
      <div v-else-if="loading" class="loading">
        加载中...
      </div>
      
      <div v-else-if="error" class="error">
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { userLevelAPI } from '@/api'
import { handleApiResponse } from '@/utils/request'
import TitleBadge from './TitleBadge.vue'

const props = defineProps({
  userId: {
    type: String,
    required: true
  },
  username: {
    type: String,
    default: ''
  }
})

const levelInfo = ref(null)
const titles = ref([])
const loading = ref(false)
const error = ref('')

const loadUserInfo = async () => {
  if (!props.userId) return
  
  try {
    loading.value = true
    error.value = ''
    
    // 获取用户等级信息
    const levelRes = await userLevelAPI.getUserLevel(props.userId)
    const levelData = handleApiResponse(levelRes, 'data')
    
    // 获取用户称号列表
    const titlesRes = await userLevelAPI.getUserTitles(props.userId)
    const titlesData = handleApiResponse(titlesRes, 'data')
    
    levelInfo.value = levelData
    titles.value = Array.isArray(titlesData) ? titlesData : []
    
  } catch (err) {
    console.error('获取用户信息失败:', err)
    error.value = '获取用户信息失败'
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadUserInfo()
})
</script>

<style scoped>
.user-title-display {
  max-width: 600px;
  margin: 0 auto;
}

.title-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #e5e7eb;
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #f3f4f6;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-icon {
  font-size: 24px;
}

.user-details {
  flex: 1;
}

.username {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
  color: #111827;
}

.level-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.level-badge {
  background: linear-gradient(135deg, #f59e0b 0%, #ef4444 100%);
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

.title-separator {
  color: #6b7280;
  font-weight: 600;
}

.title-text {
  color: #3b82f6;
  font-weight: 600;
  font-style: italic;
}

.titles-list h4 {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #374151;
}

.titles-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}

.title-item {
  background: #f9fafb;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 12px;
  text-align: center;
  position: relative;
  transition: all 0.2s;
}

.title-item:hover {
  background: #f3f4f6;
  border-color: #d1d5db;
}

.title-item.current {
  background: #dbeafe;
  border-color: #3b82f6;
}

.title-name {
  font-weight: 600;
  color: #374151;
}

.current-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  background: #3b82f6;
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 8px;
  font-weight: 600;
}

.loading, .error {
  text-align: center;
  padding: 20px;
  color: #6b7280;
}

.error {
  color: #ef4444;
}
</style>
